package com.wx.ssm.controller;

import com.wx.ssm.model.Account;
import com.wx.ssm.service.IAccountService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;

@Controller
@RequestMapping("account")
public class AccountController {
    @Resource
    IAccountService accountService=null;
    @RequestMapping("/index")
    public ModelAndView index(Account account){
        System.out.println("登录操作");
        if(accountService.indexAccount(account)!=null){
            System.out.println("登录成功");
            return new ModelAndView("jsp/index", null);
        }
        System.out.println("登录失败");
        return new ModelAndView("index", null);
    }
    @RequestMapping("/register")
    public ModelAndView register(Account account){
        System.out.println("注册操作");
        if(accountService.addAccount(account)){
            return new ModelAndView("index", null);
        }
        return new ModelAndView("register", null);
    }
@RequestMapping("/register")
    public String upload(MultipartFile myFile, HttpServletRequest request)throws Exception{
        //System.out.println(myFile.getOriginalFilename());//获取上传文件名
        String fileName=myFile.getOriginalFilename();


        //获取服务器下的项目绝对路径
        String filePath=request.getServletContext().getRealPath("/uploadFiles");
        File file=new File(filePath);
        if(!file.exists()){
            file.mkdir();
        }
        if(!myFile.isEmpty()){
            FileOutputStream output=new FileOutputStream(filePath+"/"+fileName);
            InputStream input=myFile.getInputStream();
            int a=0;
            byte bytes[]=new byte[1024];
            while((a=input.read(bytes))>0){
                output.write(bytes,0,a);
            }
            output.close();
            input.close();

        }


        return "jsp/index";
    }



}
